﻿using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using System.Collections.Concurrent;
using Microsoft.Office.Interop;
using Microsoft.Office.Interop.Excel;
using OfficeExcelApplication = Microsoft.Office.Interop.Excel.Application; 
using SUNRPA.CONTROLS.IMPL.util;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;

namespace SUNRPA.CONTROLS.IMPL.controls {
    public class GetRowAvaliableColumnNAction:BaseControlActions {
        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            GetRowAvaliableColumnNParam? param = item.ControlParam as GetRowAvaliableColumnNParam;
            string wbObject = param.ReadObject;
            Workbook wb = this.GetObjVar<ExcelModel>(ref wbObject, globleData).WorkBookValue as Workbook;
            string outVar = param.LineNumVar;
            if(wb == null) { 
                throw new RpaRuntimeException(String.Format("Excel对象{0}不存在", wbObject));
            }

            string sheetName = param.SheetName;
            this.GetStringVar(ref sheetName, globleData);
            ExcelUtil excelUtil = new ExcelUtil();
            excelUtil.SetApp(wb);

            string rowString = param.RowNum;
            this.GetStringVar(ref rowString, globleData);
            int rowNum = excelUtil.GetRowNum(rowString, sheetName);
            
            int columnNum = excelUtil.GetFisrtUsableColumnInRow(sheetName, rowNum);
            this.saveVar(globleData, outVar, new IntModel(outVar, columnNum), DefaultContents.RUNNING_PARAM);
            return currentIndex;
        }
    }
}
